package com.smc.web;

import com.alibaba.fastjson.JSON;
import com.smc.dao.SchoolInfoDao;
import com.smc.model.SchoolInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.EOFException;
import java.io.IOException;

@Controller
public class SchoolInfoAction {
    @Autowired
    private SchoolInfoDao schoolInfoDao;

    private String errorCode = "0000";

    @RequestMapping("/addSchoolInfo")
    public void add(HttpServletRequest request, HttpServletResponse response){
        SchoolInfo schoolInfo = new SchoolInfo();
        schoolInfo.setId(Integer.valueOf(request.getParameter("id")));
        schoolInfo.setSchoolName(request.getParameter("name"));
        schoolInfo.setStr1(request.getParameter("str1"));
        schoolInfo.setStr2(request.getParameter("str2"));
        schoolInfo.setStr3(request.getParameter("str3"));
        schoolInfoDao.insert(schoolInfo);
        try {
            response.getWriter().println(errorCode);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/findSchoolInfoById")
    public void find(HttpServletRequest request, HttpServletResponse response){
        SchoolInfo schoolInfo = schoolInfoDao.selectById(Integer.valueOf(request.getParameter("Id")));
        String jsonString = JSON.toJSONString(schoolInfo);
        try {
            response.getWriter().println(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/deleteById")
    public void deleteById(HttpServletRequest request ,HttpServletResponse response){
        schoolInfoDao.deleteById(Integer.valueOf(request.getParameter("Id")));
        try {
            response.getWriter().println(errorCode);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/deleteByModel")
    public void deleteByModel(HttpServletRequest request ,HttpServletResponse response){
        SchoolInfo schoolInfo = new SchoolInfo();
        schoolInfo.setId(Integer.valueOf(request.getParameter("Id")));
        schoolInfo.setSchoolName(request.getParameter("name"));
        try {
            schoolInfoDao.deleteByModel(schoolInfo);
        } catch (EOFException e) {
            errorCode="9999"+e;
        }
        try {
            response.getWriter().println(errorCode);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/updateByModel")
    public void updateByModel(HttpServletRequest request ,HttpServletResponse response){
        SchoolInfo schoolInfo = new SchoolInfo();
        schoolInfo.setId(Integer.valueOf(request.getParameter("Id")));
        schoolInfo.setSchoolName(request.getParameter("name"));
        schoolInfo.setStr1(request.getParameter("str1"));
        schoolInfo.setStr2(request.getParameter("str2"));
        schoolInfo.setStr3(request.getParameter("str3"));
        schoolInfoDao.updateByModel(schoolInfo);
        try {
            response.getWriter().println(errorCode);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
